package com.example.demo.controller;

import com.alibaba.fastjson.JSONArray;
import com.example.demo.dao.ActivitiesAreaGoodDao;
import com.example.demo.dao.ActivityesareaDao;
import com.example.demo.dao.CommodityListDao;
import com.example.demo.dao.PictureSheetOfMerchandiseDao;
import com.example.demo.pojo.*;
import com.example.demo.service.ActivitiesAreaGoodService;
import com.example.demo.service.RepertorygoodsService;
import com.example.demo.service.WarehouseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@Api(value = "仓库管理员管理页面",tags = "仓库管理员管理页面")
public class ActivitiesAreaGoodController {
    @Autowired
    private ActivitiesAreaGoodService activitiesAreaGoodService;
    @Autowired
    private WarehouseService warehouseService;
    @Autowired
    private CommodityListDao commodityListDao;
    @Autowired
    private ActivitiesAreaGoodDao activitiesAreaGoodDao;
    @Autowired
    private ActivityesareaDao activityesareaDao;
    @Autowired
    PictureSheetOfMerchandiseDao pictureSheetOfMerchandiseDao;
    @Autowired
    private RepertorygoodsService repertorygoodsService;
    //查询活动
    @RequestMapping(value = "/selectByActivity",method = RequestMethod.GET)
    @ApiOperation(value = "查看每个仓库活动商品",notes ="查看每个仓库活动商品" )
    @ApiImplicitParam(name = "activityesareaid",value ="活动id" )
    public String selectAll( HttpSession session, Model model){
        Backgrounduser backgrounduser= (Backgrounduser) session.getAttribute("houtaiUser");
        Warehouse warehouse=warehouseService.selectByUserId( backgrounduser.getBackgrounduserid());
        List<Repertorygoods> repertorygoods=repertorygoodsService.selectBy(warehouse.getRepertoryid());
        List<CommodityList> lists=new ArrayList<>();
        //查找全部商品
        for (int i = 0; i <repertorygoods.size() ; i++) {
            CommodityList commodityList=commodityListDao.selectByPrimaryKey(repertorygoods.get(i).getGoodsid());
            lists.add(commodityList);
        }
        model.addAttribute("quanbu",lists);


        List<ActivitiesAreaGood> activitiesAreaGoodList=activitiesAreaGoodDao.selectAll(warehouse.getRepertoryid());
        List<Activityesarea> activityesareas=new ArrayList<>();
        List<CommodityList> commodityLists=new ArrayList<>();
        //保证活动id的唯一性
        for (int i = 0; i <activitiesAreaGoodList.size()-1 ; i++) {
            for (int j = activitiesAreaGoodList.size()-1; j >i ; j--) {
                if (activitiesAreaGoodList.get(i).getRepertoryid().equals(activitiesAreaGoodList.get(j).getActivityesareaid())){
                    activitiesAreaGoodList.remove(j);
                }
            }
        }
        //查找仓库的所有活动
        for (int i = 0; i <activitiesAreaGoodList.size() ; i++) {
            Activityesarea activityesarea=activityesareaDao.selectByPrimaryKey(activitiesAreaGoodList.get(i).getRepertoryid());
            List<ActivitiesAreaGood> activitiesAreaGoods=activitiesAreaGoodService.selectByrepertoryid(warehouse.getRepertoryid(),activitiesAreaGoodList.get(i).getActivityesareaid());
            System.out.println("仓库和活动同查出======"+activitiesAreaGoods.get(0).getGoodsid()+".."+activitiesAreaGoods.get(1).getGoodsid());
            for (int j = 0; j <activitiesAreaGoods.size() ; j++) {
                CommodityList commodityList=commodityListDao.selectByPrimaryKey(activitiesAreaGoods.get(j).getGoodsid());
                System.out.println(commodityList.getGoodsid());
                commodityLists.add(commodityList);
            }
            for (int j = 0; j <commodityLists.size() ; j++) {
                int B=commodityLists.get(j).getGoodsid();
                PictureSheetOfMerchandise pictureSheetOfMerchandise=new PictureSheetOfMerchandise();
                pictureSheetOfMerchandise.setIss(1);
                pictureSheetOfMerchandise.setGoodsid(B);
                List<PictureSheetOfMerchandise> PI=pictureSheetOfMerchandiseDao.selectByGoodis(pictureSheetOfMerchandise);
                commodityLists.get(j).setPicturepath(PI.get(0).getPictureaddress());
            }
            activityesarea.setCommodityLists(commodityLists);
            activityesareas.add(activityesarea);
        }
        //得到仓库商品
        model.addAttribute("huodonggoods",activityesareas);

        return "huodong";
    }

    //添加每个仓库活动商品
    @RequestMapping(value = "/insertactivityGoods",method = RequestMethod.GET)
    @ApiOperation(value = "添加每个仓库活动商品",notes ="添加每个仓库活动商品" )
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsId",value ="商品id" ),
            @ApiImplicitParam(name = "activityesAreaId",value ="活动id" )
    })
    public String insert(int goodsId,int activityesAreaId,HttpSession session,Model model){
        Backgrounduser backgrounduser= (Backgrounduser) session.getAttribute("houtaiUser");
        Warehouse warehouse=warehouseService.selectByUserId( backgrounduser.getBackgrounduserid());
        ActivitiesAreaGood activitiesAreaGood=new ActivitiesAreaGood();
        activitiesAreaGood.setGoodsid(goodsId);
        activitiesAreaGood.setActivityesareaid(activityesAreaId);
        activitiesAreaGood.setRepertoryid(warehouse.getRepertoryid());
        int flag=activitiesAreaGoodService.insert(activitiesAreaGood);
        //selectAll(session,model);
       return "huodong";
    }
}
